import xml.sax
from com.zjs.zjsqueue import zurlpathqueue,zurlhookqueue

from com.zjs.util.download import request
from bs4 import BeautifulSoup as BS

class HookHandler(xml.sax.ContentHandler):
    
    hook = None
    
    def __init__(self):
        self.curenttag = ""
        self.lasttag = ""
        self.fathertag=""
        self.taskname = ""
        self.homeurl = ""
        self.hook = Hookbean()
        self.taglist = []

    # 元素开始调用
    def startElement(self, tag, attributes):
        self.taglist.append(tag)
        self.curenttag = tag
        if tag == "task":
            self.taskname = attributes["taskname"]
        elif tag == "hook":
            self.fathertag=tag
        elif self.fathertag == "hook":
            if tag == "handle":
                last_tag = self.get_last_tag()
                if last_tag == 'hook':
                    self.hook.field = attributes["return-field"]
                    self.hook.type = attributes["return-type"]
                elif last_tag == 'element':
                    self.hook.field = attributes["return-field"]
                    self.hook.type = attributes["return-type"]
                    self.add_hook(self.hook.lastelement.target_value)
            elif tag == 'element':
                #如果父类是hook
                last_tag = self.get_last_tag()
                if last_tag == 'hook':
                    e = Element()
                    e.target_attrs = attributes["target-attrs"]
                    e.target_element = attributes["target-element"]
                    e.attrs_key = attributes["attrs-key"]
                    e.attrs_value = attributes["attrs-value"]
                    url = self.get_hook()
                    html=request.get(url,3)
                    soup=BS(html,"lxml")
                    e.currentobj = soup.find(e.target_element, attrs={e.attrs_key:e.attrs_value})
                    
                    self.hook.fatherelement=e
                    self.hook.currentelement=e
                    self.hook.lastelement=e
                elif last_tag == 'element':
                    last_obj = self.hook.lastelement.currentobj
                    e = {}
                    e.target_element = attributes["target-element"]
                    e.attrs_key = attributes["attrs-key"]
                    e.attrs_value = attributes["attrs-value"]
                    e.target_attrs = attributes["target-attrs"]
                    if e.target_attrs != None :
                        if e.attrs_key != None and e.attrs_value != None:
                            e.currentobj = last_obj.find(e.target_element, attrs={e.attrs_key:e.attrs_value})
                        else:
                            e.currentobj = last_obj.find(e.target_element)
                            e.target_value = e.currentobj[e.target_attrs]
                    self.hook.lastelement=e

    # 元素结束调用
    def endElement(self, tag):
        self.taglist.pop()
        self.lasttag = tag

    # 读取字符时调用
    def characters(self, content):
        if self.curenttag == 'home-url':
            self.homeurl = content
            self.add_hook(self.homeurl)
        if self.fathertag == 'hook':
            if self.curenttag == "handle":
                if content.strip() != "":
                    self.hook.code += '\n'+content
            
        pass

    def endDocument(self):
        self.excute()
    
    def startDocument(self):
        return "sdfsd"
    
    
    def excute(self):
        if self.hook.code != '':
            code  = self.hook.code
            globals={}
            exec(code,globals)
            if self.hook.type == 'list':
                result = globals[self.hook.field]
                for obj in list(result):
                    zurlpathqueue.put(self.taskname, str(obj))
                    
    def add_hook(self,url):
        zurlhookqueue.put(self.taskname,url)
        
    def get_hook(self):
        return zurlhookqueue.get(self.taskname)
    
    def get_last_tag(self):
        if len(self.taglist) == 0 or len(self.taglist) == 1:
            return ""
        return self.taglist[len(self.taglist)-2]
    

class Hookbean():
    field = ""
    type = ""
    code = ""
    hooktype = ""
    currentelement=None
    lastelement=None
    fatherelement=None
    
class Element():
    target_element=None 
    target_attrs=None
    attrs_key=None
    attrs_value=None
    fatherelement=None
    childelement=[]
    
    currentobj=None
    
    target_value=None
    
    
    